package com.ohs.ohsoa.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ohs.ohsoa.system.dto.MeetingroomDto;
import com.ohs.ohsoa.system.entity.Meetingroom;
import com.ohs.ohsoa.system.mapper.MeetingroomMapper;
import com.ohs.ohsoa.system.vo.MeetingroomVo;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MeetingroomServiceImpl extends ServiceImpl<MeetingroomMapper, Meetingroom> implements MeetingroomService {

    @Resource
    private MeetingroomMapper meetingroomMapper;

    @Resource
    private MessageSource messageSource;

    @Override
    public List<MeetingroomVo> meetingroomSelectList(PageParam<MeetingroomDto> pageParam) {

        //获取对象
        MeetingroomDto meetingroomDto = pageParam.getSearchbean();
        //分页
        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());
        return meetingroomMapper.meetingroomSelectList(meetingroomDto);
    }

    @Override
    public MeetingroomVo meetingroomSelect(MeetingroomDto meetingroomDto) {

        return meetingroomMapper.meetingroomSelect(meetingroomDto.getMrId());
    }

    @Override
    public Boolean meetingroomInsert(MeetingroomDto meetingroomDto) {

        //判断会议室名称是否重复
        Boolean flg = noSameName(meetingroomDto);
        if (!flg) {
            throw new BizException("E00013", messageSource.getMessage("E00013", null, null));
        }

        Meetingroom meetingroom = new Meetingroom();
        BeanUtils.copyProperties(meetingroomDto, meetingroom);
        return this.save(meetingroom);
    }

    @Override
    public Boolean meetingroomDelete(MeetingroomDto meetingroomDto) {
        return meetingroomMapper.meetingroomDelete(meetingroomDto.getMrids());
    }

    @Override
    public Boolean meetingroomUpdate(MeetingroomDto meetingroomDto) {

        //判断会议室名称是否重复
        Boolean flg = noSameName(meetingroomDto);
        if (!flg) {
            throw new BizException("E00013", messageSource.getMessage("E00013", null, null));
        }

        return meetingroomMapper.meetingroomUpdate(meetingroomDto);
    }

    @Override
    public List<MeetingroomVo> meetingroomDrop(MeetingroomDto meetingroomDto) {
        if (meetingroomDto.getGroupEnterpriseGuid() == null) {
            return meetingroomMapper.meetingroomDropAll();
        } else {
            return meetingroomMapper.meetingroomDrop(meetingroomDto.getGroupEnterpriseGuid());
        }
    }

    /**
     * 判断会议室名称是否重复
     *
     * @param meetingroomDto 会议室名称
     * @return 是否重复
     */
    public Boolean noSameName(MeetingroomDto meetingroomDto) {

        QueryWrapper<Meetingroom> queryWrapper = new QueryWrapper<>();
        if (meetingroomDto.getMrId() == null) {
            queryWrapper.eq("group_enterprise_guid", meetingroomDto.getGroupEnterpriseGuid())
                    .eq("mr_name", meetingroomDto.getMrName())
                    .eq("delflg", "1");
        } else {
            queryWrapper.eq("group_enterprise_guid", meetingroomDto.getGroupEnterpriseGuid())
                    .eq("mr_name", meetingroomDto.getMrName())
                    .ne("mr_id", meetingroomDto.getMrId())
                    .eq("delflg", "1");
        }

        if (this.count(queryWrapper) == 0) {
            return true;
        }

        return false;
    }
}
